/*
 * Main.fx
 *
 * Created on Aug 3, 2009, 9:04:29 PM
 */

package iplayer;

import javafx.async.RunnableFuture;
import javafx.async.JavaTaskBase;
import iplayer.AsyncGetIplayerOperation;
import combobox.*;

import javafx.scene.paint.Color;
import javafx.scene.shape.Rectangle;
import javafx.scene.Group;
import javafx.scene.control.Label;
import javafx.scene.control.TextBox;
import iplayer.AsyncGetIplayerStreamInfo;
import javafx.scene.image.Image;
import org.jfxtras.scene.control.Shelf;

import javafx.animation.transition.RotateTransition;
import javafx.animation.KeyFrame;
import javafx.scene.Node;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.effect.Lighting;
import javafx.scene.effect.Shadow;
import javafx.scene.effect.light.DistantLight;
import javafx.scene.paint.LinearGradient;
import javafx.scene.paint.Stop;
import javafx.scene.shape.Circle;
import javafx.scene.shape.ShapeSubtract;
import javafx.stage.Stage;
import javafx.stage.StageStyle;



import javafx.scene.text.*;
import javafx.scene.control.CheckBox;

import java.lang.Void;
import javafx.animation.Timeline;
import javafx.animation.Interpolator;
import javafx.scene.image.ImageView;
import javafx.scene.effect.PerspectiveTransform;
import javafx.util.Math;
import javafx.util.Properties;


import javafx.io.Storage;
import javafx.scene.effect.Glow;

import javax.swing.JFileChooser;

import javafx.ext.swing.SwingComponent;
import java.io.File;
import java.lang.System;
import javafx.scene.layout.ClipView;
import javafx.scene.control.ScrollBar;



/**
 * @author stephengalbraith
 */
def GET_IPLAYER_LOC_KEY="get_iplayer_location";
def GET_FFMPEG_LOC_KEY="get_ffmpeg_location";
def GET_FLVSTREAMER_LOC_KEY="get_flvstreamer_location";
def OUTPUT_LOC_KEY="output_location";
var currentGetIPlayerLocation:String;
var currentFlvstreamerLocation:String;
var currentFfmpegLocation:String;
var currentOutputLocation:String;

var results:Program[];

var recording:GetIplayerRecord[] on replace{println("rebuild");rebuildRecordingGroup()};

var selectedProgram:Program on replace{println("new program selected {selectedProgram.episode}-{selectedProgram.series}")};

public class GetIplayer extends JavaTaskBase, LateReturnable
{
    public var searchType:String;
    public var channelType:String;
    public var programType:String;
    public var valueReturned:String;
    var peer:AsyncGetIplayerOperation;
    override function create():RunnableFuture
    {
        peer= new AsyncGetIplayerOperation(this,currentGetIPlayerLocation,searchType,channelType,programType);
    }

    override  function setValue(value:Object)
    {
        insert value as Program into results;
    }

    override function start()
    {
        results=[];
        valueReturned="staring...";
        super.start();
    }

    function kill():Void
    {
        peer.kill();
    }



}

public class GetIplayerStreamInfo extends JavaTaskBase
{
    var peer:AsyncGetIplayerStreamInfo;
    var program:Program;
    override function create():RunnableFuture
    {
        peer= new AsyncGetIplayerStreamInfo(program,currentGetIPlayerLocation);
    }
}

public class GetIplayerRecord extends JavaTaskBase, LateReturnable
{
    var peer:AsyncGetIplayerRecord;
    var program:Program;
    var mode:String[];
    var downloaded:Double;
    override function create():RunnableFuture
    {
        peer= new AsyncGetIplayerRecord(this,program,currentGetIPlayerLocation,
                                        currentFlvstreamerLocation,
                                        currentFfmpegLocation,currentOutputLocation,mode);
    }

    override function setValue(value:Object)
    {
        downloaded= value as Double;
    }

}



var radioStation:String[]=["Radio One","Radio Two","All"];
var tvChannels:String[]=["BBC One","BBC Two","BBC Three","BBC Four","BBC HD","All"];
var both:String[]=["BBC One","BBC Two","BBC Three","BBC Four","BBC HD","Radio One","Radio Two","All"];
var types:String[]=["TV","Radio","All"];

var disableSearch:Boolean=false;
var channelCombo:ComboBox=ComboBox
    {
          translateX:145
          translateY:30
          items:tvChannels;
          disable: bind disableSearch;
          onSelect: function(selectedItem):Void{}
        }


var typesCombo:ComboBox=ComboBox
        {
          translateX:10
          translateY:30
          items:types;
          disable: bind disableSearch;
          onSelect: function(selectedItem):Void
          {
            if (selectedItem.equals(types[0]))
            {
                channelCombo.items=tvChannels;
                channelCombo.select(0);
            }
            else if (selectedItem.equals(types[1]))
            {
                channelCombo.items=radioStation;
                channelCombo.select(0);
            }
            else if (selectedItem.equals(types[2]))
            {
                channelCombo.items=both;
                channelCombo.select(0);
            }


          }
        }



var textControl:TextBox=TextBox
        {
           translateX:285
           translateY:30
           width:100
           disable: bind disableSearch;
        }




var breadCrumb:Group=Group
{
    content:
    [
        Rectangle
        {
            stroke:Color.WHITESMOKE;
            arcHeight:5
            arcWidth:5;
            translateX:5
            translateY:10
            width:120
            height:60
        },
        Rectangle
        {
            stroke:Color.WHITESMOKE;
            arcHeight:5
            arcWidth:5;
            translateX:10
            translateY:0
            width:80
            height:17
        },
        Label
        {
            text:"Type"
            translateY:0
            translateX:15
            textFill:Color.WHITESMOKE
        },
        typesCombo,
        Rectangle
        {
            stroke:Color.WHITESMOKE;
            arcHeight:5
            arcWidth:5;
            translateX:135
            translateY:10
            width:140
            height:60
        },
        Rectangle
        {
            stroke:Color.WHITESMOKE;
            arcHeight:5
            arcWidth:5;
            translateX:145
            translateY:0
            width:90
            height:17
        },
        Label
        {
            text:"Station"
            translateY:0
            translateX:150
            textFill:Color.WHITESMOKE
        },
        channelCombo,
        Rectangle
        {
            stroke:Color.WHITESMOKE;
            arcHeight:5
            arcWidth:5;
            translateX:280
            translateY:10
            width:140
            height:60
        },
        Rectangle
        {
            stroke:Color.WHITESMOKE;
            arcHeight:5
            arcWidth:5;
            translateX:295
            translateY:0
            width:90
            height:17
        },
        Label
        {
            text:"Program"
            translateY:0
            translateX:300
            textFill:Color.WHITESMOKE
        },
        textControl

    ]
};

var availableQualityModesForSelectedProg:Group=Group{
                                content:
                                if (null!=selectedProgram)
                                {
                                       for (mode in selectedProgram.modes)
                                       {
                                        CheckBox{
                                          translateX:10;
                                          translateY: 10+(10*indexof mode);
                                          allowTriState:false;
                                          text:"{mode} ({selectedProgram.modeSizes[indexof mode]})"}
                                       }
                                 }
                                 else
                                 {
                                     Text{content:"no modes"};
                                 }



                            }
var showQualityControlPanel:Boolean=false;
var disableCoverFlow:Boolean=false;
var selectedQualityModes:String[] on replace{println("new modes {for (mode in selectedQualityModes){{mode}}}")};
var coverflow:Shelf=Shelf{
    translateX:20
                        translateY:240
                        height:200
                        width:650
 imageUrls: bind for (thumbnailURL in results){thumbnailURL.thumbnailURL};
 imageNames: bind for (thumbnailURL in results){thumbnailURL.name};
 thumbnailHeight:150
 thumbnailWidth:200
 placeholder: Image { url:"{__DIR__}static.jpg"; width:200; height:150};
 reflection:false;
 disable:bind disableCoverFlow;
 onImageClicked: function(event)
 {
     println("changing available group");
     selectedQualityModes=[];
     println("got a mode!");
     availableQualityModesForSelectedProg=Group{
                                content:
                                if (null!=selectedProgram)
                                {
                                       for (mode in selectedProgram.modes)
                                       {
                                           
                                        var checkBox:CheckBox=CheckBox{
                                            blocksMouse:true;
                                            onMouseClicked:function(event){
                                                if (checkBox.selected)
                                                {
                                                    insert mode into selectedQualityModes
                                                }
                                                else
                                                {
                                                    delete mode from selectedQualityModes;
                                                }

                                            }

                                          translateX:10;
                                          translateY: 10+(15*indexof mode);
                                          allowTriState:false;
                                          text:"{mode} ({selectedProgram.modeSizes[indexof mode]})"}
                                       }
                                 }
                                 else
                                 {
                                     Text{content:"no selectedProgram"};
                                 }



                            }
                            disableCoverFlow=true;
     showQualityControlPanel=true;
     println("made it visible");
 }
 action:function(event):Void{
     selectedProgram=results[event.index];
     }
}

var imageViewGroup:ImageView[];

var imageRecordingGroup:Group=Group{
    content: for (recordingTask in recording)
    {
            Group
            {
                    translateX:10 + 200*(indexof recordingTask);
            translateY:5;
                content:[ ImageView{

            image:Image{
                url:recordingTask.program.thumbnailURL;
            }

            },
            Rectangle{
              translateY:30;
              fill:Color.BLACK;
              opacity:0.75;
              width:150;
              height:35;
            },
            Label {
                translateX:45;
                translateY:40;
                textFill:Color.RED;
                text: bind "{recordingTask.downloaded}%";
                width:50;
            },

            Rectangle{
                translateY:80;
                width: bind 150*(recordingTask.downloaded/100) ;
                height: 5;
                fill: LinearGradient{startX: 0.0, startY: 0.0, endX: 0.0, endY: 1.0
                        proportional: false
                        stops: [
                        Stop { offset: 0.0 color: Color.LIGHTPINK },
                        Stop { offset: 0.3 color: Color.PLUM },
                        Stop { offset: 0.7 color: Color.TOMATO }
                        ]
                     }
            }
            ]
            }

        
    }

}

function rebuildRecordingGroup():Void{
   imageRecordingGroup=Group{
    content: for (recordingTask in recording)
    {
            Group
            {
                    translateX:10 + 200*(indexof recordingTask);
            translateY:5;
                content:[ ImageView{

            image:Image{
                url:recordingTask.program.thumbnailURL;
            }

            },
            Rectangle{
              translateY:30;
              fill:Color.BLACK;
              opacity:0.75;
              width:150;
              height:35;
            },
            Label {
                translateX:45;
                translateY:40;
                textFill:Color.RED;
                text: bind "{recordingTask.downloaded}%";
                width:50;
            },

            Rectangle{
                translateY:80;
                width: bind 150*(recordingTask.downloaded/100) ;
                height: 5;
                fill: LinearGradient{startX: 0.0, startY: 0.0, endX: 0.0, endY: 1.0
                        proportional: false
                        stops: [
                        Stop { offset: 0.0 color: Color.LIGHTPINK },
                        Stop { offset: 0.3 color: Color.PLUM },
                        Stop { offset: 0.7 color: Color.TOMATO }
                        ]
                     }
            }
            ]
            }


    }

}
}

function getIPlayerLocation(key:String):String{
    var storage:Storage=Storage{source:"get_iplayer.config"};
    var inputStream=storage.resource.openInputStream();
    var properties:Properties=Properties{};
    properties.load(inputStream);
    var location:String=properties.get(key);
    inputStream.close();
    return location;
}

function saveIPlayerLocation(key:String,location:String):Void{
    var storage:Storage=Storage{source:"get_iplayer.config"};
    var inputStream=storage.resource.openInputStream();
    var properties:Properties=Properties{};
    properties.load(inputStream);
    inputStream.close();
    var outputStream=storage.resource.openOutputStream(true);

    properties.put(key,location);
    properties.store(outputStream);
    outputStream.close();
}

def fileChooser= new JFileChooser();

var config:Group=Group{
        visible:false;
        var glowAmountOk:Float=0.0;
        
         content: [ Rectangle{
                 translateX:20
                 translateY:240
                 height:200
                 width:650
                 arcHeight:20
                 arcWidth:20
                 stroke:Color.WHITE
                 blocksMouse:true
                 },
                 Label{
                     translateX:100
                     translateY:250
                     textFill:Color.WHITE
                     text:"get_iplayer currently at:"
                 },
                 Label{
                     translateX:100
                     translateY:270
                     width:300
                     textFill:Color.WHITE
                     text:bind if (null==currentGetIPlayerLocation){"not defined"}else{currentGetIPlayerLocation};
                 },
                 Group{
                         var glowAmountFileBrowser:Float=0.0;
                        effect:Glow{level: bind glowAmountFileBrowser}
                        translateX:400
                        translateY:270
                        content:[
                         Rectangle
                         {
                             width:60
                             height:20
                             onMouseEntered:function(event){glowAmountFileBrowser=0.5;}
                                onMouseExited:function(event){glowAmountFileBrowser=0.0;}
                                onMouseClicked:function(event){
                                        fileChooser.setDialogTitle("Select get_iplayer");
                                        fileChooser.setFileSelectionMode(JFileChooser.FILES_ONLY);
                                        fileChooser.setCurrentDirectory(new File(System.getProperty("user.home")));
                                        SwingComponent.wrap(fileChooser);
                                        var value = fileChooser.showOpenDialog(null);
                                        if (value==JFileChooser.APPROVE_OPTION)
                                        {
                                            currentGetIPlayerLocation=fileChooser.getSelectedFile().getPath();
                                            saveIPlayerLocation(GET_IPLAYER_LOC_KEY,currentGetIPlayerLocation);
                                        }

                                        }
                         },
                         Label{
                             textFill:Color.WHITE
                             text:"Change"
                         }
                      ]
                 },
                 // todo make this a class...
                 // flvstreamer location
                 Label{
                     translateX:100
                     translateY:300
                     textFill:Color.WHITE
                     text:"flvstreamer currently at:"
                 },
                 Label{
                     translateX:100
                     translateY:320
                     width:300
                     textFill:Color.WHITE
                     text:bind if (null==currentFlvstreamerLocation){"not defined"}else{currentFlvstreamerLocation};
                 },
                 Group{
                         var glowAmountFileBrowser:Float=0.0;
                        effect:Glow{level: bind glowAmountFileBrowser}
                        translateX:400
                        translateY:320
                        content:[
                         Rectangle
                         {
                             width:60
                             height:20
                             onMouseEntered:function(event){glowAmountFileBrowser=0.5;}
                                onMouseExited:function(event){glowAmountFileBrowser=0.0;}
                                onMouseClicked:function(event){
                                        fileChooser.setDialogTitle("Select flvstreamer");
                                        fileChooser.setFileSelectionMode(JFileChooser.FILES_ONLY);
                                        fileChooser.setCurrentDirectory(new File(System.getProperty("user.home")));
                                        SwingComponent.wrap(fileChooser);
                                        var value = fileChooser.showOpenDialog(null);
                                        if (value==JFileChooser.APPROVE_OPTION)
                                        {
                                            currentFlvstreamerLocation=fileChooser.getSelectedFile().getPath();
                                            saveIPlayerLocation(GET_FLVSTREAMER_LOC_KEY,currentFlvstreamerLocation);
                                        }

                                        }
                         },
                         Label{
                             textFill:Color.WHITE
                             text:"Change"
                         }
                      ]
                 },
                 // finally the ffmpeg
                 Label{
                     translateX:100
                     translateY:340
                     textFill:Color.WHITE
                     text:"ffmpeg currently at:"
                 },
                 Label{
                     translateX:100
                     translateY:360
                     width:300
                     textFill:Color.WHITE
                     text:bind if (null==currentFfmpegLocation){"not defined"}else{currentFfmpegLocation};
                 },
                 Group{
                         var glowAmountFileBrowser:Float=0.0;
                        effect:Glow{level: bind glowAmountFileBrowser}
                        translateX:400
                        translateY:360
                        content:[
                         Rectangle
                         {
                             width:60
                             height:20
                             onMouseEntered:function(event){glowAmountFileBrowser=0.5;}
                                onMouseExited:function(event){glowAmountFileBrowser=0.0;}
                                onMouseClicked:function(event){
                                        fileChooser.setDialogTitle("Select ffmpeg");
                                        fileChooser.setFileSelectionMode(JFileChooser.FILES_ONLY);
                                        fileChooser.setCurrentDirectory(new File(System.getProperty("user.home")));
                                        SwingComponent.wrap(fileChooser);
                                        var value = fileChooser.showOpenDialog(null);
                                        if (value==JFileChooser.APPROVE_OPTION)
                                        {
                                            currentFfmpegLocation =fileChooser.getSelectedFile().getPath();
                                            saveIPlayerLocation(GET_FFMPEG_LOC_KEY,currentFfmpegLocation);
                                        }

                                        }
                         },
                         Label{
                             textFill:Color.WHITE
                             text:"Change"
                         }
                      ]
                 },
                 // output directory
                 Label{
                     translateX:100
                     translateY:380
                     textFill:Color.WHITE
                     text:"Output to:"
                 },
                 Label{
                     translateX:100
                     translateY:400
                     width:300
                     textFill:Color.WHITE
                     text:bind if (null==currentOutputLocation){"not defined"}else{currentOutputLocation};
                 },
                 Group{
                         var glowAmountFileBrowser:Float=0.0;
                        effect:Glow{level: bind glowAmountFileBrowser}
                        translateX:400
                        translateY:400
                        content:[
                         Rectangle
                         {
                             width:60
                             height:20
                             onMouseEntered:function(event){glowAmountFileBrowser=0.5;}
                                onMouseExited:function(event){glowAmountFileBrowser=0.0;}
                                onMouseClicked:function(event){
                                        fileChooser.setDialogTitle("Choose Output Directory");
                                        fileChooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
                                        fileChooser.setCurrentDirectory(new File(System.getProperty("user.home")));
                                        SwingComponent.wrap(fileChooser);
                                        var value = fileChooser.showOpenDialog(null);
                                        if (value==JFileChooser.APPROVE_OPTION)
                                        {
                                            currentOutputLocation =fileChooser.getSelectedFile().getPath();
                                            saveIPlayerLocation(OUTPUT_LOC_KEY,currentOutputLocation);
                                        }

                                        }
                         },
                         Label{
                             textFill:Color.WHITE
                             text:"Change"
                         }
                      ]
                 },
                 Group{
                     effect:Glow{level: bind glowAmountOk}
                     translateX:500
                     translateY:410
                     content:[
                         Rectangle{
                             width:50
                             height:25;
                             onMouseEntered:function(event){glowAmountOk=0.5;}
                                onMouseExited:function(event){glowAmountOk=0.0;}
                                onMouseClicked:function(event){
                                     coverflow.visible=true;
                                     config.visible=false;
                                        }
                         },

                         Label{
                             text:"OK"
                             textFill:Color.WHITE
                         }
                     ]
                 }

                 ];
        }

function getPerspectiveTransform(t:Number):PerspectiveTransform {
        var width = 100;
        var height = 80;
        var radius = width/2;
        var back = height/10;
        return PerspectiveTransform {
            ulx: radius - Math.sin(t)*radius     uly: 0 - Math.cos(t)*back
            urx: radius + Math.sin(t)*radius     ury: 0 + Math.cos(t)*back
            lrx: radius + Math.sin(t)*radius     lry: height - Math.cos(t)*back
            llx: radius - Math.sin(t)*radius     lly: height + Math.cos(t)*back
        }
    }


var scrollBar:ScrollBar = ScrollBar{
                                       vertical:false
                                   unitIncrement:5
                                   min:0
                                   max: bind Math.max(1,  imageRecordingGroup.boundsInLocal.width-590)
                                   translateX:10
                                translateY:95
                                width:590

                               }

public function run()
{
        println("paths: {__DIR__}static.jpg\n {__DIR__}../getiplayer.jpg");
    var scene:Scene;
    var closeX:Node;
    var searchBreadCrumb:Node;
    var resultsTable:Node;
    var task:GetIplayer;
    var sceneX:Integer;
    var sceneY:Integer;
    println("about to get iplayer location");
    currentGetIPlayerLocation=getIPlayerLocation(GET_IPLAYER_LOC_KEY);
    currentFfmpegLocation=getIPlayerLocation(GET_FFMPEG_LOC_KEY);
    currentFlvstreamerLocation=getIPlayerLocation(GET_FLVSTREAMER_LOC_KEY);
    currentOutputLocation=getIPlayerLocation(OUTPUT_LOC_KEY);
    println("Got get iplayer location {currentGetIPlayerLocation}");
    typesCombo.select(0);
    var stage:Stage =Stage {
    style: StageStyle.TRANSPARENT
    scene: 
        scene=Scene {
        width:750;
        height: 700
        var startX:Float;
        var startY:Float;
        fill: Color{red:0.0 green:0.0 blue:0.0 opacity:0}
        content: [
            ShapeSubtract {
                cache: true
                onMousePressed:function(event){
                        startX=event.screenX-stage.x;
                        startY=event.screenY-stage.y;
                        }
                onMouseDragged:function(event){
                        stage.x=event.screenX-startX;
                        stage.y=event.screenY-startY;
                        }
                effect: Lighting {
                    specularConstant: 0.5
                    bumpInput: Shadow { radius: 20 }
                    light: DistantLight {azimuth: -90}
                }
                a: bind [
                    Rectangle {
                        x:0
                        y:50
                        width: scene.width - 50
                        height: scene.height - 50
                        arcWidth: 100
                        arcHeight: 100
                    }
                ]
                b: bind [
                    Circle {
                        centerX: scene.width - 50
                        centerY: 50
                        radius: 40
                    }

                ]
                fill: LinearGradient {
                    endX:0
                    stops: [
                        Stop { offset: 0, color: Color.web("#444444") }
                        Stop { offset: 0.7, color: Color.web("#666666") }
                        Stop { offset: 1, color: Color.web("#333333") }
                    ]
                }

            }

            Circle {
                cache: true
                effect: Lighting {
                    specularConstant: 0.5
                    bumpInput: Shadow { radius: 20 }
                    light: DistantLight {azimuth: -90}
                }
                centerX: bind scene.width - 50
                centerY: 50
                radius: 35
                fill: Color.web("#0099f1")
                onMouseClicked: function (e) {
                    stage.close()
                }
                onMouseEntered: function(e) {
                    RotateTransition { node: closeX toAngle: 90 duration: 0.5s }.play();
                }
                onMouseExited: function(e) {
                    RotateTransition { node: closeX toAngle: 0 duration: 0.25s }.play();
                }
            }



            closeX = Group {
                content: [
                    Rectangle {
                        x: bind scene.width - 70
                        y: 47
                        width: 40
                        height: 6
                        arcWidth: 6
                        arcHeight: 6
                        fill: Color.WHITE
                        rotate: 45
                    }
                    Rectangle {
                        x: bind scene.width - 70
                        y: 47
                        width: 40
                        height: 6
                        arcWidth: 6
                        arcHeight: 6
                        fill: Color.WHITE
                        rotate: -45
                    }
                ]
            }

            Group {
                var perspectiveTime:Float=Math.PI/2;
                content: [
                    Label {
                        translateX:20
                        translateY:100
                        textFill:Color.WHITESMOKE
                        text:"GetIPlayer UI in JavaFX"
                    },
                    Group{
                        translateX:400
                        translateY:100
                        var glowAmount:Float=0.0;
                        effect:Glow{level: bind glowAmount}
                        content:[
                        Rectangle{
                                opacity:0.0;
                                width:100;
                                height:20;
                                onMouseEntered:function(event){glowAmount=0.85;}
                                onMouseExited:function(event){glowAmount=0.0;}
                                onMouseClicked:function(event){
                                    println("clicked");
                                    coverflow.visible=false;
                                    config.visible=true;
                                    }
                        },
                        Label {
                            textFill:Color.WHITE
                            text:"Configuration"
                        }]
                    },
                    Group{
                        translateX:50
                        translateY:120
                        content:[breadCrumb]
                        }
                    Group{
                        translateX:550
                        translateY:130
                        var highlight:Color=Color.DARKOLIVEGREEN;
                        var shadow:Color=Color.ANTIQUEWHITE;
                        var current:Color=shadow;
                        var searchingTimeline:Timeline=Timeline{repeatCount:Timeline.INDEFINITE
                               keyFrames: [KeyFrame{time:0ms values: perspectiveTime=>Math.PI/2},
                                           KeyFrame{time:200ms values: perspectiveTime=>Math.PI},
                                           KeyFrame{time:400ms values: perspectiveTime=>(3*Math.PI)/2},
                                           KeyFrame{time:600ms values: perspectiveTime=>(2*Math.PI)}]
                            }
                        content:[Group{

                            effect: bind getPerspectiveTransform(perspectiveTime);
                            content:[ Circle
                                {
                                    effect: Lighting {
                                        specularConstant: 0.5
                                        bumpInput: Shadow{}
                                        light: DistantLight {azimuth: -90}
                                    }
                                    radius: 40
                                    fill: bind current

                                }, ImageView{image:Image{url:"{__DIR__}iplayer_logo.gif"}}
                                ]
                        },
                        Rectangle{
                                width:100
                                height:80
                                opacity:0
                                onMouseEntered: function(event){ current=highlight};
                                    onMouseExited: function(event){ current=shadow};
                                    onMouseClicked: function(event){ task= Main.GetIplayer{
                                            onDone: function(){searchingTimeline.stop(); perspectiveTime=Math.PI/2;disableSearch=false;}
                                        };
                                        disableSearch=true;
                                        textControl.commit();
                                        task.searchType=typesCombo.selectedItem as String;
                                        task.channelType = channelCombo.selectedItem as String;
                                        task.programType = textControl.text;
                                        println("searching for program {task.programType}.");
                                        task.start();
                                        searchingTimeline.playFromStart();
                                        }
                                }]
                    }
                    ,
                    Button {
                        translateX:550
                        translateY:220
                        text:"Stop"
                        action: function(){
                            perspectiveTime=Math.PI/2;
                            task.kill();
                            }
                    }
                    ,
                    config,
                    coverflow,
                    Group{
                        translateX: bind coverflow.translateX+(coverflow.width/2)-100;
                        translateY: bind coverflow.translateY+(coverflow.height/2)-75;

                        visible:bind showQualityControlPanel;
                        blocksMouse:true;
                        content:
                        [
                            
                            Rectangle{
                                width:200;
                                height:150;
                                fill: Color.ANTIQUEWHITE;
                                opacity: 0.75;
                                stroke: Color.WHITESMOKE;
                                arcHeight:5;
                                arcWidth:5;
                            },
                            Group{
                                content: bind availableQualityModesForSelectedProg
                                },
                            Button{
                                translateX:10
                                translateY:125
                                text:"cancel"
                                blocksMouse:true;
                                action: function(){
                                    showQualityControlPanel=false;
                                    FX.deferAction(function():Void{
                                        disableCoverFlow=false;});
                                    }
                            },
                            Button{
                                translateX:115
                                translateY:125
                                text:"record"
                                blocksMouse:true;
                                action:
                                function(){
                                        var recordTask:Main.GetIplayerRecord= Main.GetIplayerRecord{
                                            onDone: function(){FX.deferAction(function():Void{delete recordTask from recording;})}
                                        };
                                        recordTask.program=selectedProgram;
                                        recordTask.mode=selectedQualityModes;
                                        recordTask.start();
                                        showQualityControlPanel=false;
                                        var imageXLoc:Integer on replace{println("animation {imageXLoc}")};
                                        var imageYLoc:Integer;
                                        var imageRotation:Integer;
                                        var imageView:ImageView=ImageView
                                        {
                                            translateX: bind imageXLoc;
                                            translateY: bind imageYLoc;
                                            rotate:bind imageRotation;
                                            visible:true;
                                            image:Image{
                                                url:recordTask.program.thumbnailURL;
                                            }

                                        }
                                        FX.deferAction(function():Void{disableCoverFlow=false;});
                                        var recordImageTransferTimeline:Timeline=Timeline{
                                            keyFrames: [ KeyFrame{ time:0ms values:[imageXLoc=>((coverflow.translateX+(coverflow.width/2)-100) as Integer), imageYLoc=>((coverflow.translateY+(coverflow.height/2)-75) as Integer), imageRotation=>0] },
                                                         KeyFrame{ time:500ms values:[imageXLoc=>100 tween Interpolator.EASEOUT,
                                                                                      imageYLoc=>355 tween Interpolator.EASEOUT,
                                                                                      imageRotation=>180 tween Interpolator.LINEAR]},
                                                         KeyFrame{ time:750ms values:[imageXLoc=>50 tween Interpolator.EASEIN,
                                                                                      imageYLoc=>580 tween Interpolator.EASEIN,
                                                                                      imageRotation=>360 tween Interpolator.LINEAR]
                                                                  action:function():Void{
                                                                    insert recordTask before recording[0];
                                                                    delete imageView from imageViewGroup}}

                                                       ]
                                            };
                                        FX.deferAction(function():Void{
                                                insert imageView into imageViewGroup;
                                                recordImageTransferTimeline.playFromStart();
                                        });
                                    }

                            }



                        ]


                    },
                    Text{
                        translateX:200
                        translateY:500
                        wrappingWidth:300
                        content: bind "{if (null==results[coverflow.index].episode) {"Nothing selected"}else{results[coverflow.index].episode}}\n{if (null==results[coverflow.index].description){""} else {results[coverflow.index].description}}"
                    },
                    Group
                    {
                        translateX:50
                        translateY:580
                        var xPos:Integer=0
                        var scrollerValue:Number on replace{
                            xPos=Math.round(scrollerValue) as Integer;
                            println("setting xPos to {xPos}");
                        }

                        content: [
                            Rectangle{
                                translateX:0;
                                translateY:0;
                                width: 610
                                height:100
                                arcHeight:15
                                arcWidth:15
                                fill:Color.BLACK
                                stroke:Color.WHITESMOKE
                                },
                                ClipView{
                                        clipX:bind scrollBar.value
                                        clipY:0
                                        width:590
                                        height:95
                                        node:bind imageRecordingGroup},
                             scrollBar


                            ]
                    },
                    Group{
                        content:bind imageViewGroup
                    }

                ]
            }
        ]
    }
}
}

